<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
</head>

<body>
<div class="layui-container">
    <div class="layui-row">
        <div class="layui-col-lg-offset3" layui-col-lg6>
            <div class="layui-form layuimini-form">
                <form class="layui-form" action="" lay-filter="userForm">

                    <div class="layui-form-item">
                        <label class="layui-form-label">ISBN:</label>
                        <div class="layui-input-block">
                            <input type="text" name="bookId" required lay-verify="required" placeholder="请输入书的ISBN号"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">书名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="bookName" required lay-verify="required" placeholder="请输入用书名"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <input type="hidden" name="id" value="" class="layui-input">

                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">作者</label>
                        <div class="layui-input-block">
                            <input type="text" name="author" required lay-verify="required" placeholder="请输入图书的作者"
                                   autocomplete="off" class="layui-input">
                        </div>

                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">数量</label>
                        <div class="layui-input-block">
                            <input type="text" name="residueNumber" required lay-verify="required" placeholder="请输入新增的数量"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">价格</label>
                        <div class="layui-input-block">
                            <input type="text" name="bookPrice" required lay-verify="required" placeholder="请输入图书的价格"
                                   autocomplete="off" class="layui-input">
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <label class="layui-form-label">图书种类</label>
                        <div class="layui-input-inline" >
                            <select id = "groupId" name="groupId" lay-filte="test"> 
                                <option value=""></option>
                            </select>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            </div>

        </div>
    </div>
</div>



<script>
    //Demo
    layui.use(['form','element','upload'], function () {
        var form = layui.form
            , $ = layui.jquery
            ,element = layui.element
            ,upload = layui.upload
            , laydate = layui.laydate;

        $.ajax({
             url:'http://localhost:8080/library/book/group',
             data:{},
             dataType: 'json',
             type:'get',
             success:function (resultData) {

               // console.log(resultData.data);
                //var arr = resultData.data.list

                        $("#groupId").append(new Option("请选择图书所属分类", ""));

                        $.each(resultData.data.list, function(index, item) {
                          //  console.log(index);
                           // console.log(item.groupId);
                        //如果是数组的话，index就是表示下标，item表示元素值。如果是对象，index表示的是对象的key值，item表示key相对应的value。
                        //，第一个参数是需要显示的值，第二个参数是option的value值
                            $('#groupId').append(new Option(item.groupName,item.groupId));

                        });

                form.render('select');  //渲染下拉框
            }
        });




        //提交
        form.on('submit(formDemo)', function (data) {
            console.log(JSON.stringify(data.field));
            $.ajax({
                "url":"http://localhost:8080/library/book/addBook",
               // "headers":{"token":token},
                "data": data.field,    
                // result 参数 表式后台响应的数据
                "success":function(result){
                    if(result.code == 200){
                        // 关闭 弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.msg("添加成功");
                        parent.layer.close(iframeIndex);

                    }else{
                        layer.msg(result.message);
                    }
                },
                "dataType":"json"
            });

            return false;
        });
    });
</script>


</body>

</html>